package com.kg.elysia.utils;

import com.kg.elysia.db.Mural;
import org.apache.poi.ss.usermodel.*;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author zj
 * @since 2025/10/20 15:25
 */
public class ExcelParser {

    public static List<Mural> parse(InputStream is) {
        List<Mural> murals = new ArrayList<>();
        try (Workbook workbook = WorkbookFactory.create(is)) {
            Sheet sheet = workbook.getSheetAt(0);

            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                Mural mural = new Mural();

                mural.setTombName(getStringValue(row.getCell(0)));
                mural.setMuralPosition(getStringValue(row.getCell(1)));
                mural.setReference(getStringValue(row.getCell(2)));
                mural.setGender(getStringValue(row.getCell(3)));
                mural.setIdentity(getStringValue(row.getCell(4)));
                mural.setScene(getStringValue(row.getCell(5)));
                mural.setUpperGarment(getStringValue(row.getCell(6)));
                mural.setLowerGarment(getStringValue(row.getCell(7)));
                mural.setAccessory(getStringValue(row.getCell(8)));
                mural.setShoes(getStringValue(row.getCell(9)));
                mural.setColor(getStringValue(row.getCell(10)));
                mural.setPattern(getStringValue(row.getCell(11)));
                mural.setPeriod(getStringValue(row.getCell(12)));
                mural.setDynasty(getStringValue(row.getCell(13)));
                mural.setInstitution(getStringValue(row.getCell(14)));
                mural.setCulture(getStringValue(row.getCell(15)));
                mural.setSocialClass(getStringValue(row.getCell(16)));
                mural.setReligion(getStringValue(row.getCell(17)));
                murals.add(mural);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return murals;
    }

    private static String getStringValue(Cell cell) {
        if (cell == null) {
            return "";
        }
        cell.setCellType(CellType.STRING);
        return cell.getStringCellValue().trim();
    }

}
